<?php
namespace Macaca\Module\Admin;

use Macaca;
use Monkey;

class Nav {

    /**
     * @var Monkey\App
     */
    public $app;

    private $modelNav; //`nav_id`, `caption`, `parent_id`, `sort`, `module_type`, `module_id`
    private $modelPriKey;
    private $modelCacheKey = 'MacacaNav';


    /**
     * @param Monkey\App $app
     */
    public function __construct($app) {
        $this->app = $app;
        $this->modelNav = new Macaca\Model\Admin\Nav($app);
        $this->modelPriKey = $this->modelNav->priKey;
    }

    /**
     * @return array //`nav_id`, `caption`, `parent_id`, `sort`, `module_type`, `module_id`
     */
    public function getNavList() {
        $result = array();
        $cache = $this->app->cache();
        if (!$cache->fetch($this->modelCacheKey, $result)) {
            $result = $this->modelNav->findAll();
            $cache->store($this->modelCacheKey, $result);
        }
        return $result;
    }

    public function getSubNav($parentId = 0) {
        $nav = $this->getNavList();
        $tree = array();
        $this->buildTree($nav, $tree, $parentId);

        return $tree;
    }

    public function getParentList($navId, $parentId) {
        $list = $this->getSubNav(0);
        $pk = $this->modelPriKey;
        $doc = $this->app->view()->document();

        $parentList[] = $doc->selectOption('创建新导航栏', 0, $parentId == 0);

        $parent = null;
        foreach ($list as $item) {
            if ($item[$pk] == $navId) {
                $parent = $item;
                continue;
            }

            if (!is_null($parent)) {
                if (strlen($parent['fix']) < strlen($item['fix'])) {
                    continue;
                }
                else {
                    $parent = null;
                }
            }

            $parentList[] = $doc->selectOption($item['fix'] . $item['caption'], $item[$pk], $parentId == $item[$pk]);
        }

        return implode("\n", $parentList);
    }

    /**
     * 获取一个空菜单项目
     * @return mixed
     */
    public function getEmptyItem() {
        $data['nav_id'] = '';
        $data['caption'] = '新菜单';
        $data['parent_id'] = '0';
        $data['sort'] = '50';
        $data['module_type'] = 'page';
        $data['module_id'] = '1';
        return $data;
    }

    public function getItemById($navId) {
        $data = $this->modelNav->find($this->modelPriKey, $navId);

        if ($data) {
            return $data;
        }
        else {
            return array();
        }
        //
        //        $data = $this->getNavList();
        //        foreach ($data as $item) {
        //            if ($item['nav_id'] == $navId) {
        //                return $item;
        //            }
        //        }
        //        return array();
    }

    public function deleteItemById($navId) {
        if ($this->modelNav->delete($this->modelPriKey, $navId)) {
            $this->resetCache();
            return true;
        }
        else {
            return false;
        }
    }

    public function saveItem($data = array()) {
        $pk = $this->modelPriKey;
        if (!empty($data[$pk]) and !is_numeric($data[$pk])) {
            return false;
        }

        $dataTemp = $this->getEmptyItem();
        unset($dataTemp[$pk]);
        foreach ($dataTemp as $k => $v) {
            if (!isset($data[$k])) {
                return false;
            }
            $dataTemp[$k] = $data[$k];
        }

        if (!empty($data[$pk])) {
            $result = $this->modelNav->updateByPriKey($data[$pk], $dataTemp);
        }
        else {
            $result = $this->modelNav->insert($dataTemp);
        }

        if ($result) {
            $this->resetCache();
        }

        return $result;
    }


    protected function resetCache() {
        $this->app->cache()->delete($this->modelCacheKey);
    }

    protected function buildTree(&$source, &$tree, $parentId = 0, $deep = 0, $fix = '- ') {
        $isBreak = false;
        foreach ($source as $k => $item) {

            if ($item['parent_id'] != $parentId) {
                continue;
            }

            $item['fix'] = str_repeat($fix, $deep);
            $tree[] = $item;
            unset($source[$k]);
            $this->buildTree($source, $tree, $item['nav_id'], $deep + 1);
            $isBreak = true;
            break;
        }

        if (!empty($source) and $isBreak) {
            $this->buildTree($source, $tree, $parentId, $deep);
        }
    }

} 